<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/Pub.css'/>"></link>
    <script type="text/javascript">
        var path = "${pageContext.request.contextPath}";
    </script>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- 如果出现没有效果的情况直接检查是否是因为路径的问题 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/easyui/themes/default/easyui.css"
          type="text/css"></link>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/easyui/themes/icon.css" type="text/css"></link>
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.easyui.min.js"></script>
    <style type="text/css">td {
        text-align: center
    }</style>
</head>
<body>
<%--判断是否是从订单明细过来的数据，如果是则显示进度 --%>
<c:if test="${!empty requestScope.order}" var="boo">
    <p>选择/输入送货地址->订单明细--><font color="red" style="font-family:bold;">选择/添加收货地址</font>->确认订单->网银付款或到货付款->查收货物</p>
</c:if>
<c:if test="${!boo}">
    <p>以下是你的收货地址列表：</p>
</c:if>

<table id="table" class="table table-striped">
    <caption>收货地址列表</caption>
    <thead>
    <tr>
        <th style="text-align: center">收货人姓名/地址</th>
        <th style="text-align: center">电话</th>
        <th style="text-align: center">邮编</th>
        <th style="text-align: center">默认地址</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${addrs}" var="addr">

        <tr>
            <td id="name">
                    ${addr.name}
            </td>
            <td id="phone">
                    ${addr.phone}
            </td>
            <td id="zip">
                    ${addr.zip}
            </td>
            <td>
                <input type="radio"
                       <c:if test="${addr.dft==1}">checked='checked'</c:if> name="dft"
                       onclick="_chgDft(this,'${addr.id}');" value='${book.dft}' />
                <button type="button" class="btn btn-primary" onclick="_del('${addr.id}');return false;">删除</button>
            </td>
        </tr>
    </c:forEach>

    <tr>
        <td></td>
        <td></td>
        <td></td>
        <td>
            <button type="button" onclick="_toOrder();" class="btn btn-primary">下一步->订单页面</button>
            <button type="button" onclick="_add();" class="btn btn-primary">增加</button>
        </td>
    </tr>
    </tbody>
</table>
</body>
<script type="text/javascript">
    //跳转到添加页面
    function _add() {
        var url = path + "/jsp/front/address/add.jsp";
        window.open(url, "_blank", "width=410px;height=300px;");
    }

    //设置为默认收货地址
    function _chgDft(obj, id) {
        if (obj.checked == true && obj.value == 1) {//如果本来就是选中，则不做任何工作
            return;
        }
        var url = "${pageContext.request.contextPath}/address_changeToDefault.action?id=" + id;
        window.location.href = url;
    }
    //删除一个地址
    function _del(id) {
        var url = "${pageContext.request.contextPath}/address_delAddress.action?id=" + id;
        window.location.href = url;
    }

    //确定订单
    function _toOrder() {
        var dfts = document.getElementsByName("dft");
        var dft = null;
        for (var i = 0, len = dfts.length; i < len; i++) {
            if (dfts[i].checked == true) {
                dft = dfts[i];
                break;
            }
        }

        if (dft) {
            var consignee = "";
            var tr = dft.parentNode;
            if (tr.nodeName != 'TR') {
                tr = tr.parentNode;//tr
            }
           var name_addr = tr.firstChild.nextSibling.textContent.trim();
            var phone = tr.firstChild.nextSibling.nextSibling.nextSibling.textContent.trim();
            var zip = tr.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling.textContent.trim();
            consignee = name_addr+','+phone +','+zip;
            $.messager.alert('订单', '正在进入订单确认页面!', 'info', function () {
                window.location.href = "${pageContext.request.contextPath}/order_toOrder.action?consignee="+consignee.trim();
            });
        } else {
            $.messager.alert('提示', '请选择一个收货地址!', 'info', function () {

            });
        }
    }
</script>
</html>
